﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.supply
{
    public partial class ListTotalEarlyArrival : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindDDL();
                Bind();

            }
        }


        private void BindDDL()
        {
            string str = " roles_id = 6 ";

            DataSet ds = new dzs.BLL.user().GetList(str);

            this.ddl_gygly.DataSource = ds;
            this.ddl_gygly.DataTextField = "user_name";
            this.ddl_gygly.DataValueField = "user_id";
            this.ddl_gygly.DataBind();

        }

        private void Bind()
        {
            Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();
            Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();

            List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");//获取时间结算日期
            dzs.Model.configinfo c = cm[0];

            string user_id = this.ddl_gygly.SelectedItem.Value;

            string str = " user_id = " + user_id;

            if (!string.IsNullOrEmpty(this.txt_supplyDate.Text))
            {
                DateTime ghq = DateTime.Parse(this.txt_supplyDate.Text);
                #region 月供应计划起始结束时间

                DateTime monthEndTime;//本月供应计划结束时间
                DateTime monthStartTime;//本月供应计划起始时间


                int day = ghq.Day;

                if (day <= c.ygyjs)
                {
                    monthStartTime = DateTime.Parse(ghq.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                else
                {
                    monthStartTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                #endregion

                str += " and monthSupplyDate between '" + monthStartTime + "' and '" + monthEndTime + "'  ";//供应年月
            }

            DataTable dt = msm.GetListMonthSupplyTotalCount(str).Tables[0];

            DataTable dt_monthSupply = dt.Clone();

            foreach (DataRow row in dt.Rows)
            {
                DateTime gyny = DateTime.Parse(row["monthSupplyDate"].ToString());//供应年月
                DateTime ghq = DateTime.Parse(row["supplyDate"].ToString());// 供货期

                if (cm.Count == 1)
                {
                    DateTime ghqMonthStartTime;//供货期的起始时间

                    int ghqDay = ghq.Day;//供货期

                    if (ghqDay <= c.ygyjs)//当前配置结算日期，则按照上月到这个日期小于月计算，大于配置结算日期，则按照这个月到下月计算
                    {
                        ghqMonthStartTime = DateTime.Parse(ghq.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                    }
                    else
                    {
                        ghqMonthStartTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyqs + ""));

                    }

                    if (DateTime.Compare(gyny, ghqMonthStartTime) < 0)//供应年月小于供货期起始时间 早到货
                    {
                        //if (row["isUpErp"].ToString() == "False")//是否已ERP上传
                        //{
                        //    //flagEarlyGreen = false;
                        //    row_source["flagEarlyGreen"] = "T";
                        //    break;
                        //}
                        dt_monthSupply.ImportRow(row);

                    }
                }
            }

            this.lbl_complate.Text = "提前到货数量有 "
                + dt_monthSupply.Rows.Count + "条";

            this.GridView1.DataSource = dt_monthSupply;
            this.GridView1.DataBind();


            this.lbl_supplyDateWarn.Text = "*月供应计划起始日期为上月 " + c.ygyqs + " 号至当月 " + c.ygyjs + " 号";
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            Bind();
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            Bind();

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=yuegongjihua.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>当月供应计划生成及时率</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }
    }
}